<?php

/**
* AdminGuideListTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';

class  AdminGuideListTest extends WebTestCase
{
    private $url;
    private $testNAme = array();
    private $idArray = array();
    private $username = '';
    private $password = '';

    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url =$global['service_url'];
        $this->testName[] = md5(uniqid(rand(), true));
        $this->testName[] = md5(uniqid(rand(), true));
        $this->testName[] = md5(uniqid(rand(), true));

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  

  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("INSERT INTO guide (name,owner) values ( '".$this->testName[0]."','".$this->username."' )");
        $this->idArray[$this->testName[0]] = $dbh->lastInsertId();

        $dbh->exec("INSERT INTO slide (guide_id,name,owner) values ( '".$this->idArray[$this->testName[0]]."','".md5(uniqid(rand(), true))."','".$this->username."' )");
        $dbh->exec("INSERT INTO slide (guide_id,name,owner) values ( '".$this->idArray[$this->testName[0]]."','".md5(uniqid(rand(), true))."','".$this->username."' )");
        $dbh->exec("INSERT INTO slide (guide_id,name,owner,video_file) values ( '".$this->idArray[$this->testName[0]]."','".md5(uniqid(rand(), true))."','".$this->username."','jNQXAC9IVRw' )");


        $dbh->exec("INSERT INTO guide (name,owner) values ( '".$this->testName[1]."','".$this->username."' )");
        $this->idArray[$this->testName[1]] = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO guide (name,owner) values ( '".$this->testName[2]."','".$this->username."' )");
        $this->idArray[$this->testName[2]] = $dbh->lastInsertId();
        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413");         
        $this->get($this->url.'?cmd=AdminGuideList');

        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");

        $this->get($this->url.'?cmd=AdminGuideList');
    }

    public function testGuideList()
    {

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertLink('[Dodaj trasę]',$this->url."index.php?cmd=AdminAddGuide");
        $this->assertLink($this->testName[0],$this->url."index.php?cmd=AdminShowRoute&id=".$this->idArray[$this->testName[0]]);
        $this->assertLink($this->testName[1],$this->url."index.php?cmd=AdminShowRoute&id=".$this->idArray[$this->testName[1]]);
        $this->assertLink($this->testName[2],$this->url."index.php?cmd=AdminShowRoute&id=".$this->idArray[$this->testName[2]]);
        
    }

    public function testRemoveGuide()
    {

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        
        
        $this->assertLinkById('AdminRemoveGuide'.$this->idArray[$this->testName[0]],$this->url."index.php?cmd=AdminRemoveGuide&id=".$this->idArray[$this->testName[0]]);
        $this->assertLinkById('AdminRemoveGuide'.$this->idArray[$this->testName[1]],$this->url."index.php?cmd=AdminRemoveGuide&id=".$this->idArray[$this->testName[1]]);
        $this->assertLinkById('AdminRemoveGuide'.$this->idArray[$this->testName[2]],$this->url."index.php?cmd=AdminRemoveGuide&id=".$this->idArray[$this->testName[2]]);

        $this->assertLink('[Usuń]');
        $this->clickLinkById('AdminRemoveGuide'.$this->idArray[$this->testName[0]]);
        $this->assertText('Usunąć trasę '.$this->testName[0].' razem z 3 slajdami ?');
        $this->clickSubmit('Tak');

        $this->assertText('Trasa usunięta');
        
        $this->click('Lista tras',$this->url."index.php?cmd=AdminGuideList");

        $this->assertLink($this->testName[1],$this->url."index.php?cmd=AdminShowRoute&id=".$this->idArray[$this->testName[1]]);
        $this->assertLink($this->testName[2],$this->url."index.php?cmd=AdminShowRoute&id=".$this->idArray[$this->testName[2]]);        
        $this->assertFalse($this->clickLink($this->testName[0],$this->url."index.php?cmd=AdminShowRoute&id=".$this->idArray[$this->testName[0]]));
        
        $this->assertFalse( $this->clickLinkById('AdminRemoveGuide'.$this->idArray[$this->testName[0]]) );
    
        $this->assertTrue($this->clickLink('[Usuń]'));

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $sth = $dbh->prepare("SELECT COUNT(rowid) FROM slide WHERE guide_id = :guide_id");
        $sth->bindParam(':guide_id',$this->idArray[$this->testName[0]]);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],"0");
    }    

    public function testDoNotRemoveGuide()
    {

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        
        $this->assertLinkById('AdminRemoveGuide'.$this->idArray[$this->testName[0]],$this->url."index.php?cmd=AdminRemoveGuide&id=".$this->idArray[$this->testName[0]]);
        $this->assertLinkById('AdminRemoveGuide'.$this->idArray[$this->testName[1]],$this->url."index.php?cmd=AdminRemoveGuide&id=".$this->idArray[$this->testName[1]]);
        $this->assertLinkById('AdminRemoveGuide'.$this->idArray[$this->testName[2]],$this->url."index.php?cmd=AdminRemoveGuide&id=".$this->idArray[$this->testName[2]]);

        $this->clickLinkById('AdminRemoveGuide'.$this->idArray[$this->testName[0]]);

        $this->clickSubmit('Nie');

        $this->assertLinkById('AdminRemoveGuide'.$this->idArray[$this->testName[0]],$this->url."index.php?cmd=AdminRemoveGuide&id=".$this->idArray[$this->testName[0]]);
        $this->assertLinkById('AdminRemoveGuide'.$this->idArray[$this->testName[1]],$this->url."index.php?cmd=AdminRemoveGuide&id=".$this->idArray[$this->testName[1]]);
        $this->assertLinkById('AdminRemoveGuide'.$this->idArray[$this->testName[2]],$this->url."index.php?cmd=AdminRemoveGuide&id=".$this->idArray[$this->testName[2]]);

        $this->assertLink('[Dodaj trasę]',$this->url."index.php?cmd=AdminAddGuide");
        $this->assertLink($this->testName[0],$this->url."index.php?cmd=AdminShowRoute&id=".$this->idArray[$this->testName[0]]);
        $this->assertLink($this->testName[1],$this->url."index.php?cmd=AdminShowRoute&id=".$this->idArray[$this->testName[1]]);
        $this->assertLink($this->testName[2],$this->url."index.php?cmd=AdminShowRoute&id=".$this->idArray[$this->testName[2]]);
        

    }  


    public function tearDown() 
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("DELETE FROM slide WHERE guide_id = ".$this->idArray[$this->testName[0]]);
        $dbh->exec("DELETE FROM guide WHERE name LIKE '".$this->testName[0]."'" );
        $dbh->exec("DELETE FROM guide WHERE name LIKE '".$this->testName[1]."'" );
        $dbh->exec("DELETE FROM guide WHERE name LIKE '".$this->testName[2]."'" );

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminGuideList&logout=1');
        $dbh = null;
    }
    
}
?>
